package com.miyu.client1.service.bookAdmin.checkRule;

import com.alibaba.fastjson.JSONObject;
import com.miyu.client1.service.magic.impl.initDataCommon;
import com.miyu.client1.service.magic.impl.initSaveData;
import com.miyu.client1.utils.bookUtils.JXPageUtils;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class checkRule implements PageProcessor {
    private static String  regex = "\\(([^}]*)\\)";//匹配小括号


    public static void checkRule(Request request) {

        Spider.create(new checkRule()).addRequest(request)
                .addPipeline(new checkRuleData())
                .thread(50)
                .run();
    }
    @SneakyThrows
    @Override
    public void process(Page page) {

        Request request = page.getRequest();
        String url=page.getUrl().get();//得到url，起始为http://www.147xs.org/
        //图书规则必带 根据规则解析相关内容
        String rule  = page.getRequest().getExtra("rule");
        JSONObject jo = new JSONObject();

       // jo.put("rule",jxRulePage(page, rule));
        jo.put("rule", JXPageUtils.JXPageRuleByCheckRule(page, rule));

        page.putField("checkRule",jo);

    }

    @Override
    public Site getSite() {
        return site;
    }

    //爬虫配置信息设置
    private Site site = new Site()
            .setCharset("utf-8")    //设置编码
            .setSleepTime(2)        //设置抓取间隔
            .setTimeOut(10000)         //设置超时时间
            .setRetrySleepTime(3000)      //设置重试时间
            .setRetryTimes(5);       //设置重试次数


    //解析规则 说明
    /**
     * 规则分三段 1 正常取值  2 字符截取   3  字符替换
     * 1.使用 tg. 分割 如果只有正常取值可不加 标志符位
     * 2.字符截取规则 tg.sp(0@2) 字符取substring(0,2)取值, lp(.@1) 指 split(".")分割取 第一个 & 符 合并取值 暂不做复杂处理 tg.sp()&lp() 格式
     * 3.rp 字符替换 tg.rp(我@你) "我"替换 "你",tg.rp(#nbsp@"") 去空格
     * //li[@class='author']/a/text()tg.sp(0@3)&lp(:@1)tg.rp(#nbsp@"")
     *  增加href 取值规则 tg.lk(.@0) 最后取链接 ()取值第几个链接 默认 0 链接默认最后取值 不做分割数据
     */
    private String jxRule(Selectable page , String ruleStr){
        String rs = "";
        if(StringUtils.isNotBlank(ruleStr)){
            String [] tg = ruleStr.split("tg.");
            if(tg.length>0){
                rs =  page.xpath(tg[0]).get();
                for (int i = 1; i < tg.length; i++) {
                    String [] sa = tg[i].split("&");
                    for (String str:sa){
                        if(str.indexOf("lk")>-1){
                            Selectable st = page.xpath(tg[0]);
                            return   rs = regLink(st,page,str);
                        }

                        rs =  reg(rs,str);
                    }
                }
            }else {
                rs =  page.xpath(ruleStr).get();
            }

        }
        return  rs;
    }

    /**
     * 参数page
     * @param page
     * @param ruleStr
     * @return
     */
    private String jxRulePage(Page page ,String ruleStr){
        String rs = "";
        if(StringUtils.isNotBlank(ruleStr)){
            String [] tg = ruleStr.split("tg.");
            if(tg.length>0){
                rs =  page.getHtml().xpath(tg[0]).get();
                for (int i = 1; i < tg.length; i++) {
                    String [] sa = tg[i].split("&");
                    for (String str:sa){
                        if(str.indexOf("lk")>-1){
                            Selectable st = page.getHtml().xpath(tg[0]);
                            return rs = regLinkPage(st,page,str);
                        }

                        rs =  reg(rs,str);
                    }
                }
            }else {
                rs =  page.getHtml().xpath(ruleStr).get();
            }

        }
        return  rs;
    }

    private String regLinkPage(Selectable ob, Page page, String str) {
        String rs = "";
        //取括号内容 之后再分割 取值
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(str);
        String group="";

        while(matcher.find()){
            group = matcher.group(1);
            String [] s = group.split("@");
            if(str.indexOf("lk")>-1){
                if(s.length>1)
                    rs =  ob.xpath("//a").links().get();
                    // rs = ob.replace(s[0],s[1]);
                else
                    rs = ob.toString();
            }
        }
        return  rs;
    }

    private String regLink(Selectable ob, Selectable page, String str) {
        String rs = "";
        //取括号内容 之后再分割 取值
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(str);
        String group="";

        while(matcher.find()){
            group = matcher.group(1);
            String [] s = group.split("@");
            if(str.indexOf("lk")>-1){
                if(s.length>1)
                    rs =  ob.xpath("//a").links().get();
                    // rs = ob.replace(s[0],s[1]);
                else
                    rs = ob.toString();
            }
        }
        return  rs;
    }


    private String reg(String ob ,String str){
        String rs = ob;
        //取括号内容 之后再分割 取值
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(str);
        String group="";

        while(matcher.find()){
            group = matcher.group(1);
        }
        String [] s = group.split("@");
        if(str.indexOf("sp")>-1){
            if(s.length>1)
                rs = ob.substring(Integer.valueOf(s[0]),Integer.valueOf(s[1]));
            else
                rs = ob.substring(Integer.valueOf(s[0]),ob.length());
        }else if(str.indexOf("lp")>-1){
            if(s.length>1)
                if(ob.split(s[0]).length>1)
                    rs = ob.split(s[0])[Integer.valueOf(s[1])];
                else
                    rs = ob;
        }else if(str.indexOf("rp")>-1){
            if(s.length>1)
                rs = ob.replace(s[0],s[1]);
            else
                rs = ob;
        }else if(str.indexOf("lk")>-1){
            if(s.length>1)
                rs = ob.replace(s[0],s[1]);
            else
                rs = ob;
        }
        return  rs;
    }

}
